//
let request=require("request");

let io=require('socket.io-client');

let socket=io("http://127.0.0.1:9999/");

socket.on("event", async (a)=>{console.log('chance:',a);
    let body=await doRequest(`http://127.0.0.1:9999/i/${a.notify}`,{verb:"finish", result:"OK"});
    console.log(body);
});
socket.emit("tokenify", "a");


function doRequest(url, post) { // GET/POST json and retrieve json.
    console.log(`url:${url}, sending:`,post);
    if (typeof post != "undefined")
        return new Promise(function (resolve, reject) {
            request({
                url, method:"POST", json:true,
                header: {"Content-type": "application/json; charset=UTF-8"},
                body: post
            }, function (error, res, body) {
                if (!error && res.statusCode == 200) {
                    console.log('answer:',body);
                    resolve(body);
                } else {
                    resolve({error});
                }
            });
        });
    else
        return new Promise(function (resolve, reject) {
            request(url, function (error, res, body) {
                if (!error && res.statusCode == 200) {
                    let json=JSONbig.parse(body);
                    console.log('answer:',json);
                    resolve(json);
                } else {
                    resolve({error});
                }
            });
        });
}

async function test(){
    let body=await doRequest("http://127.0.0.1:9999/i/test",{hello:"world"});
    console.log(body);
}

test();